package com.example.com.common.view.dialog;


import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;

import com.example.com.common.R;


public class ChooseDialog extends BaseDialog {
	View.OnClickListener leftBtnListener,rightBtnListener;
	String leftBtnText,rightBtnText;
	String title;
	CharSequence message;
	int gravity = Gravity.CENTER_VERTICAL;

	public ChooseDialog(Context context, CharSequence message, String title) {
		super(context, R.style.translucentDialog);
		this.title = title;
		this.message = message;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_choose);

		if(!TextUtils.isEmpty(title)){
			((TextView) findViewById(R.id.tv_dialog_title)).setText(title);
		}else{
			findViewById(R.id.tv_dialog_title).setVisibility(View.GONE);
			findViewById(R.id.dialog_title_line).setVisibility(View.GONE);
		}
		TextView content = ((TextView)findViewById(R.id.tv_dialog_content));
		content.setText(message);
		content.setGravity(gravity);
		Button button = (Button)findViewById(R.id.btn_choose_left);
		if(!TextUtils.isEmpty(leftBtnText))
			button.setText(leftBtnText);
		button.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				dismiss();
				if(leftBtnListener!=null){
					leftBtnListener.onClick(v);
				}
			}
		} );
		button = (Button)findViewById(R.id.btn_choose_right);
		if(!TextUtils.isEmpty(rightBtnText))
			button.setText(rightBtnText);
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
				if(rightBtnListener!=null)
					rightBtnListener.onClick(v);
			}
		});
		findViewById(R.id.linear_outside).setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if(canceledOnTouchOutside)
					dismiss();
				return false;
			}
		});
		
	}

	public void setLeftButton(String text,
			View.OnClickListener listener) {
		leftBtnText = text;
		leftBtnListener = listener;
	}
	public void setRightButton(String text,
			View.OnClickListener listener) {
		rightBtnText = text;
		rightBtnListener = listener;
	}
	
	public void setGravity(int gravity){
		this.gravity = gravity;
	}
	private boolean canceledOnTouchOutside = true;
	@Override
	public void setCanceledOnTouchOutside(boolean cancel) {
		this.canceledOnTouchOutside = cancel;
	}
}
